import { createWebHistory, createRouter } from "vue-router";
import { message } from "ant-design-vue";
import { useAuthorizationStore } from "@/stores/authorization";

const history = createWebHistory();
const router = createRouter({
  history,
  routes: [
    {
      //登录页面
      path: "/login",
      name: "Login",
      component: () => import("@/views/login/Login.vue"),
      meta: {
        title: "登录页",
        transition: "animate__lightSpeedInRight",
      },
    },
    {
      //后台
      path: "/",
      name: "Layout",
      component: () => import("@/components/layout/Home.vue"),
      redirect: "/index",
      meta: {
        title: "首页",
        transition: "animate__lightSpeedInRight",
      },
      children: [
        {
          //首页
          path: "/index",
          name: "Index",
          component: () => import("@/views/index/Index.vue"),
          meta: {
            title: "首页",
            transition: "animate__lightSpeedInLeft",
          },
        },
        {
          //商家页面
          path: "/business",
          name: "Business",
          component: () => import("@/views/business/Business.vue"),
          meta: {
            title: "商户管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商家页面
          path: "/storecheck",
          name: "StoreCheck",
          component: () => import("@/views/storecheck/StoreCheck.vue"),
          meta: {
            title: "审核管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //普通用户页面
          path: "/user",
          name: "User",
          component: () => import("@/views/user/User.vue"),
          meta: {
            title: "用户管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //品牌管理页面
          path: "/brand",
          name: "Brand",
          component: () => import("@/views/brand/Brand.vue"),
          meta: {
            title: "品牌管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //品牌管理页面
          path: "/notice",
          name: "Notice",
          component: () => import("@/views/notice/Notice.vue"),
          meta: {
            title: "公告管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //分类管理页面
          path: "/category",
          name: "Category",
          component: () => import("@/views/category/Category.vue"),
          meta: {
            title: "分类管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/store",
          name: "Store",
          component: () => import("@/views/store/Store.vue"),
          meta: {
            title: "店铺管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/goods",
          name: "Goods",
          component: () => import("@/views/goods/Goods.vue"),
          meta: {
            title: "商品管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/allgoods",
          name: "AllGoods",
          component: () => import("@/views/allgoods/AllGoods.vue"),
          meta: {
            title: "商品管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/online",
          name: "Online",
          component: () => import("@/views/online/Online.vue"),
          meta: {
            title: "在线用户",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/order",
          name: "Order",
          component: () => import("@/views/order/Order.vue"),
          meta: {
            title: "订单管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/addgoods",
          name: "AddGoods",
          component: () => import("@/views/goods/add/AddGoods.vue"),
          meta: {
            title: "新增商品",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/feedback",
          name: "FeedBack",
          component: () => import("@/views/feedback/FeedBack.vue"),
          meta: {
            title: "反馈管理",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/oper",
          name: "Oper",
          component: () => import("@/views/oper/Oper.vue"),
          meta: {
            title: "系统操作",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品管理页面
          path: "/exception",
          name: "Exception",
          component: () => import("@/views/exception/Exception.vue"),
          meta: {
            title: "系统异常",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //店铺评论
          path: "/storecomment",
          name: "StoreComment",
          component: () => import("@/views/store/comment/StoreComment.vue"),
          meta: {
            title: "店铺评论",
            transition: "animate__lightSpeedOutRight",
          },
        },
        {
          //商品评论
          path: "/goodscomment",
          name: "GoodsComment",
          component: () => import("@/views/goods/comment/GoodsComment.vue"),
          meta: {
            title: "商品评论",
            transition: "animate__lightSpeedOutRight",
          },
        },
      ],
    },
    {
      path: "/:pathMatch(.*)*",
      name: "NotFound",
      component: () => import("@/views/404/NotFound.vue"),
    },
  ],
});

// GOOD
router.beforeEach((to, from, next) => {
  const authorizationStore = useAuthorizationStore();
  const authorization = authorizationStore.authorization;

  if (to.path === "/login") {
    if (authorization) {
      next("/");
    } else {
      next();
    }
  } else if (authorization) {
    next();
  } else {
    next("/login");
    message.warning("无权限，请登录！");
  }
});

export default router;
